package com.ajview.common.core.exception.base;

import com.ajview.common.core.domain.enums.ResultCodeEnum;
import com.ajview.common.utils.MessageUtils;
import com.ajview.common.utils.StringUtils;

/**
 * PROJECT_NAME: ajview-smile
 *
 * @DESCRIPTION: 基础异常
 * @DATE 2022-12-17 下午 02:58
 * @Author zhangjunjie
 */
public class BaseReponseException extends RuntimeException {

    private static final long serialVersionUID = 630201199303173861L;

    /**
     * 所属模块
     */
    private String module;

    /**
     * 错误码
     */
    private String messageCode;

    /**
     * 响应状态码
     */
    private Integer code;

    /**
     * 错误码对应的参数
     */
    private Object[] args;

    /**
     * 错误消息
     */
    private String defaultMessage;

    /**
     * 真实的异常信息
     */
    private Exception e;

    public BaseReponseException(String module, String messageCode, Object[] args, Integer code, String defaultMessage, Exception e) {
        this.module = module;
        this.messageCode = messageCode;
        this.args = args;
        this.defaultMessage = defaultMessage;
        this.code = (code == null || code == 0) ? ResultCodeEnum.REQUEST_ERROR.getCode() : code;
        this.e = (e == null) ? this : e;
    }

    public BaseReponseException(String module, String messageCode, Object[] args) {
        this(module, messageCode, args, null, null, null);
    }

    public BaseReponseException(String module, String messageCode) {
        this(module, messageCode, null, null, null, null);
    }

    public BaseReponseException(String module, ResultCodeEnum resultCodeEnum) {
        this(module, resultCodeEnum.getMessageCode(), null, resultCodeEnum.getCode(), resultCodeEnum.getMessage(), null);
    }

    @Override
    public String getMessage() {
        String message = null;
        if (!StringUtils.isEmpty(messageCode)) {
            message = MessageUtils.message(messageCode, args);
        }
        if (StringUtils.isEmpty(message)) {
            message = defaultMessage;
        }
        return message;
    }

    public String getModule() {
        return module;
    }

    public String getMessageCode() {
        return messageCode;
    }

    public Object[] getArgs() {
        return args;
    }

    public String getDefaultMessage() {
        return defaultMessage;
    }

    public Integer getCode() {
        return code;
    }

    public Exception getE() {
        return e;
    }
}
